package com.triones.api.controller.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.triones.api.entity.HelpCategorizeDO;
import com.triones.api.entity.HelpDO;
import com.triones.api.entity.vo.HelpCategorizeVO;
import com.triones.api.entity.vo.HelpVO;
import com.triones.api.service.HelpService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 帮助中心
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/help")
public class HelpController extends BaseController {

    private final HelpService helpService;

    /**
     * 查询帮助分类列表
     */
    @SaCheckPermission("system:helpCategorize:pageList")
    @GetMapping("/categorize/list")
    public TableDataInfo<HelpCategorizeVO> list(HelpCategorizeDO dto, PageQuery pageQuery) {
        return helpService.selectPageHelpCategorizeList(dto, pageQuery);
    }

    /**
     * 查询帮助分类详细
     *
     * @param id
     */
    @SaCheckPermission("system:helpCategorize:query")
    @GetMapping(value = "/categorize/{id}")
    public R<HelpCategorizeVO> getInfo(@PathVariable Long id) {
        return R.ok(helpService.selectHelpCategorizeById(id));
    }

    /**
     * 新增帮助分类
     */
    @SaCheckPermission("system:helpCategorize:add")
    @PostMapping("/categorize")
    public R<Void> add(@Validated @RequestBody HelpCategorizeDO dto) {
        if (!helpService.checkHelpCategorizeUnique(dto)) {
            return R.fail("新增帮助分类'" + dto.getZhCategorizeName() + "'失败，帮助分类已存在");
        }
        helpService.insertHelpCategorize(dto);
        return R.ok();
    }

    /**
     * 修改帮助分类
     */
    @SaCheckPermission("system:helpCategorize:edit")
    @PutMapping("categorize")
    public R<Void> edit(@Validated @RequestBody HelpCategorizeDO dto) {
        if (!helpService.checkHelpCategorizeUnique(dto)) {
            return R.fail("修改帮助分类'" + dto.getZhCategorizeName() + "'失败，帮助分类已存在");
        }
        helpService.updateHelpCategorize(dto);
        return R.ok();
    }

    /**
     * 删除帮助分类
     *
     * @param id
     */
    @SaCheckPermission("system:helpCategorize:remove")
    @DeleteMapping("/categorize/{id}")
    public R<Void> remove(@PathVariable Long id) {
        helpService.deleteHelpCategorizeByIds(id);
        return R.ok();
    }

    /**
     * 获取选择框列表
     */
    @GetMapping("/categorize/optionselect")
    public R<List<HelpCategorizeVO>> optionSelect() {
        List<HelpCategorizeVO> dictTypes = helpService.selectHelpCategorizeAll();
        return R.ok(dictTypes);
    }

    /**
     * 查询列表
     */
    @SaCheckPermission("system:help:list")
    @GetMapping("/data/list")
    public TableDataInfo<HelpVO> listData(HelpDO dto, PageQuery pageQuery) {
        return helpService.list(dto, pageQuery);
    }

    /**
     * 查询详细
     *
     * @param id
     */
    @SaCheckPermission("system:help:query")
    @GetMapping(value = "/data/{id}")
    public R<HelpVO> getDataInfo(@PathVariable Long id) {
        return R.ok(helpService.selectDataById(id));
    }


    /**
     * 新增
     */
    @SaCheckPermission("system:help:add")
    @PostMapping("/data")
    public R<Void> addData(@Validated @RequestBody HelpDO dto) {
        if (!helpService.checkDataTitleUnique(dto)) {
            return R.fail("新增数据'" + dto.getZhTitle() + "'失败，标题已存在");
        }
        helpService.insertData(dto);
        return R.ok();
    }

    /**
     * 修改保存
     */
    @SaCheckPermission("system:help:edit")
    @PutMapping("/data")
    public R<Void> editData(@Validated @RequestBody HelpDO dto) {
        if (!helpService.checkDataTitleUnique(dto)) {
            return R.fail("修改数据'" + dto.getZhTitle() + "'失败，标题已存在");
        }
        helpService.updateData(dto);
        return R.ok();
    }

    /**
     * 删除
     *
     * @param id
     */
    @SaCheckPermission("system:help:remove")
    @DeleteMapping("/data/{id}")
    public R<Void> removeData(@PathVariable Long id) {
        helpService.deleteDataByIds(id);
        return R.ok();
    }
}
